I am developing a Download Manager. It works fine for one single file but, for multiple files it is not working properly: what is wrong?
When I enter first url, it works fine, but when I click on pause and try a second url after clicking on download, the first file's progress bar starts from 0, and the first file starts downloading automatically without clicking on resume.
When I click on pause, both files pause, when I click on resume both files start downloading. Why this is happening? When I click pause on first url downloading window, I want only the first file to pause, and when I click on resume, only that file should be resumed.
ViewModel:
namespace DownloadManager
{
public class ViewModel : INotifyPropertyChanged
{
public ICommand _Download { get; set; }
public ICommand Pause { get; set; }
public ICommand Resume { get; set; }
public ViewModel()
{
_Download = new Command(Start, canExecuteMethod);
Pause = new Command(Pause_Download, canExecuteMethod);
Resume = new Command(Resume_Download, canExecuteMethod);
}
private bool canExecuteMethod(object parameter)
{
return true;
}
private void Start(object parameter)
{
new Add_URL().Button_Click_1(link);
}
private void Pause_Download(object parameter)
{
new Add_URL().Pause();
}
private void Resume_Download(object parameter)
{
new Add_URL().Resume();
}
public event PropertyChangedEventHandler? PropertyChanged;
private string link;
public string Link
{
get { return link; }
set { link = value; OnPropertyChanged(Link); }
}
private void OnPropertyChanged(string property)
{
if(PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(property));
}
}
}
}
Model:
<pre>public static bool CheckForInternetConnection()
{
try
{
using (var client = new WebClient())
using (var stream = client.OpenRead("http://www.google.com"))
{
return true;
}
}
catch
{
return false;
}
}
Download fw;
static int row = 0;
public void Button_Click_1(string link)
{
Uri uri = null;
if (CheckForInternetConnection())
{
{
uri = new Uri(link);
if (!uri.IsFile)
{
WarningUrl.Content = "File Found";
file = true;
}
}
}
else
{
WarningUrl.Content = "No Internet";
}
if (file)
{
String name = link;
Add_URL a = new Add_URL();
String FileName = System.IO.Path.GetFileName(uri.AbsolutePath);
MainWindow mainwindow = new MainWindow();
string check = mainwindow.Check(FileName);
MainWindow m = Application.Current.MainWindow as MainWindow;
string extension = System.IO.Path.GetExtension(FileName);
var s = GetFileSize(uri);
int x = Int32.Parse(s);
var fileSizeInMegaByte = Math.Round(Convert.ToDouble(s) / 1024.0 / 1024.0, 2).ToString();
String size = fileSizeInMegaByte + " MB";
String time = DateTime.Now.ToString("g");
Download download = new Download();
var method2 = new Action<int>(i =>
{
download.pb.Value = i;
MainWindow m = Application.Current.MainWindow as MainWindow;
if (i< 100)
{
m.change(download, i.ToString(), row, FileName);
}
download.FileName1.Content = FileName;
});
var method3 = new Action(() =>
{
download.pb.Value = 100;
string a = "100";
m.change(download, a, row, FileName);
row++;
download.Status.Content = "Completed";
download.Close();
});
fw = new Download(name, path + @"\" + FileName, x);
CancellationTokenSource tokenSource = new CancellationTokenSource();
CancellationToken token = tokenSource.Token;
mre.Set();
new Thread(() =>
{
while (!fw.Done)
{
mre.WaitOne();
double receive = double.Parse(fw.BytesWritten.ToString());
double Filesize = x;
double Percentage = receive / Filesize * 100;
int per = int.Parse(Math.Truncate(Percentage).ToString());
try
{
Thread.Sleep(300);
this.Dispatcher.Invoke(() =>
{
method2(per);
if (fw.Done)
{
method3();
}
});
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}).Start();
fw.Start(); ---> throws exception here
download.Show();
m.AddItem(a, FileName, extension, time, size);
Close();
}
}
public void Pause()
{
fw.Pause1();
}
public void Resume()
{
fw.Start();
}
What I have tried:
Check out this image :--
https://i.stack.imgur.com/e6hZB.png[
^]
I have tried above things but didn't work properly.
What can I do?
Please help!!